package arsmodeller.model.impl;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.impl.EFactoryImpl;
import org.eclipse.emf.ecore.plugin.EcorePlugin;

import arsmodeller.model.Factory;
import arsmodeller.model.Link;
import arsmodeller.model.ObjectProcessingDiagram;
import arsmodeller.model.Package;
import arsmodeller.model.TObject;
import arsmodeller.model.Thing;
import arsmodeller.model.Process;

public class FactoryImpl extends EFactoryImpl implements Factory {
	/**
	 * Creates the default factory implementation. <!-- begin-user-doc --> <!--
	 * end-user-doc -->
	 * 
	 * @generated
	 */
	public static Factory init() {
		try {
			Factory theOPMFactory = (Factory) EPackage.Registry.INSTANCE
					.getEFactory("www.vainolo.com/phd/opm");
			if (theOPMFactory != null) {
				return theOPMFactory;
			}
		} catch (Exception exception) {
			EcorePlugin.INSTANCE.log(exception);
		}
		return new FactoryImpl();
	}

	/**
	 * Creates an instance of the factory. <!-- begin-user-doc --> <!--
	 * end-user-doc -->
	 * 
	 * @generated
	 */
	public FactoryImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public EObject create(EClass eClass) {
		switch (eClass.getClassifierID()) {
		case Package.OPM_OBJECT_PROCESS_DIAGRAM:
			return createOPMObjectProcessDiagram();
		case Package.OPM_OBJECT:
			return createOPMObject();
		case Package.OPM_PROCESS:
			return createOPMProcess();
		case Package.OPM_LINK:
			return createOPMLink();
		case Package.OPM_THING:
			return createOPMThing();
		default:
			throw new IllegalArgumentException("The class '" + eClass.getName()
					+ "' is not a valid classifier");
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public Object createFromString(EDataType eDataType, String initialValue) {
		switch (eDataType.getClassifierID()) {
		case Package.RECTANGLE:
			return createRectangleFromString(eDataType, initialValue);
		case Package.POINT:
			return createPointFromString(eDataType, initialValue);
		default:
			throw new IllegalArgumentException("The datatype '"
					+ eDataType.getName() + "' is not a valid classifier");
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public String convertToString(EDataType eDataType, Object instanceValue) {
		switch (eDataType.getClassifierID()) {
		case Package.RECTANGLE:
			return convertRectangleToString(eDataType, instanceValue);
		case Package.POINT:
			return convertPointToString(eDataType, instanceValue);
		default:
			throw new IllegalArgumentException("The datatype '"
					+ eDataType.getName() + "' is not a valid classifier");
		}
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public ObjectProcessingDiagram createOPMObjectProcessDiagram() {
		ObjectProcessingDiagramImpl opmObjectProcessDiagram = new ObjectProcessingDiagramImpl();
		return opmObjectProcessDiagram;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public TObject createOPMObject() {
		TObjectImpl opmObject = new TObjectImpl();
		return opmObject;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public Process createOPMProcess() {
		ProcessImpl opmProcess = new ProcessImpl();
		return opmProcess;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public Link createOPMLink() {
		LinkImpl opmLink = new LinkImpl();
		return opmLink;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public Thing createOPMThing() {
		ThingImpl opmThing = new ThingImpl();
		return opmThing;
	}

	/**
	 * <!-- begin-user-doc --> Create a <code>Rectangle</code> instance from a
	 * <code>String</code>. The expected representation is "x,y,width,height".
	 * Illegal representations will return a null value. <!-- end-user-doc -->
	 * 
	 * @generated NOT
	 */
	public Rectangle createRectangleFromString(EDataType eDataType,
			String initialValue) {
		if (initialValue == null) {
			return null;
		}
		initialValue.replaceAll("\\s", "");
		String[] values = initialValue.split(",");
		if (values.length != 4) {
			return null;
		}

		Rectangle rect = new Rectangle();
		try {
			rect.setLocation(Integer.parseInt(values[0]),
					Integer.parseInt(values[1]));
			rect.setSize(Integer.parseInt(values[2]),
					Integer.parseInt(values[3]));
		} catch (NumberFormatException e) {
			EcorePlugin.INSTANCE.log(e);
			rect = null;
		}
		return rect;
	}

	/**
	 * <!-- begin-user-doc --> Convert a <code>Rectangle</code> to a
	 * <code>String</code> representation. The <code>Rectangle</code> is
	 * represented as "x,y,width,heigth". <!-- end-user-doc -->
	 * 
	 * @generated NOT
	 */
	public String convertRectangleToString(EDataType eDataType,
			Object instanceValue) {
		if (instanceValue == null) {
			return null;
		}
		Rectangle rect = (Rectangle) instanceValue;
		return rect.x + "," + rect.y + "," + rect.width + "," + rect.height;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated NOT
	 */
	public Point createPointFromString(EDataType eDataType, String initialValue) {
		if (initialValue == null) {
			return null;
		}
		initialValue.replaceAll("\\s", "");
		String[] values = initialValue.split(",");
		if (values.length != 2) {
			return null;
		}

		Point point = new Point();
		try {
			point.setLocation(Integer.parseInt(values[0]),
					Integer.parseInt(values[1]));
		} catch (NumberFormatException e) {
			EcorePlugin.INSTANCE.log(e);
			point = null;
		}
		return point;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated NOT
	 */
	public String convertPointToString(EDataType eDataType, Object instanceValue) {
		if (instanceValue == null) {
			return null;
		}
		Point p = (Point) instanceValue;
		return p.x + "," + p.y;

	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public Package getOPMPackage() {
		return (Package) getEPackage();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @deprecated
	 * @generated
	 */
	@Deprecated
	public static Package getPackage() {
		return Package.eINSTANCE;
	}
}
